package com.shaokel.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.shaokel.commonutils.ReturnResult;
import com.shaokel.exception.shaokelException;
import com.shaokel.vod.service.VodService;
import com.shaokel.vod.utils.ConstantVoUtils;
import com.shaokel.vod.utils.InitVodCilent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author 少丶客
 * @ClassName VodController
 * @Description TODO
 * @Date 2020/6/8 18:03
 * @Vesion 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/eduvod/video")
public class VodController {

    @Autowired
    private VodService vodService;

    //上传视频
    @PostMapping("/uploadAlyiVideo")
    public ReturnResult uploadAlyiVideo(MultipartFile file){
        String videoId  = vodService.uploadAlyiVideo(file);
        return ReturnResult.ok().data("videoId ",videoId);
    }

    //根据id删除视频
    @GetMapping("/removeAlyiVideo/{id}")
    public ReturnResult removeAlyiVideo(@PathVariable String id){
        try{
            //初始化对象
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVoUtils.ACCESS_KEY_ID, ConstantVoUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(id);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return ReturnResult.ok();
        }catch(Exception e){
            e.printStackTrace();
            throw new shaokelException(20001,"删除视频失败");
        }
    }

    //批量删除视频
    @DeleteMapping("/deleteBatch")
    public ReturnResult deleteBatch(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.deleteBatch(videoIdList);
        return ReturnResult.ok();
    }

}